#include "OSAL.h"

/*任务处理函数指针*/
typedef uint8_t (*pTaskEventHandler)(uint8_t taskID, uint16_t event);
/*任务列表定义*/
static pTaskEventHandler tasksArr[] = {
    HAL_ProcessEvents,
    OSAL_Timer_ProcessEvents


};
/*OSAL的任务数量*/
static const uint8_t taskCnt = sizeof(tasksArr) / sizeof(tasksArr[0]);
/*OSAL任务的事件*/
static uint16_t events[taskCnt] = {0};
static uint8_t taskID = 0;


int activeTask = TASK_INVALID_ID;
/**
 * @fn OSAL_InitSystem
 * @brief 初始化OSAL， 添加任务
 * @param none
 * @return none
 */
void OSAL_InitSystem(void)
{
		osal_msg_init();
    OSAL_InitTasks();
}

/**
 * @fn OSAL_SystemRunning
 * @brief 轮询有无事件， 有则调用相应处理函数处理事件
 * @param none
 * @return none
 */
void OSAL_SystemRunning(void)
{
    uint8_t taskID = 0;
    uint16_t taskEvent = 0;
    for (; taskID < taskCnt; taskID++)
    {
        if (events[taskID])
            break;
    }
    if (taskID < taskCnt)
    {
				activeTask = taskID;
        taskEvent = events[taskID];
        events[taskID] = 0;
        taskEvent = tasksArr[taskID](taskID, taskEvent);
        events[taskID] |= taskEvent;
				activeTask = TASK_INVALID_ID;
    }
}

/**
 * @fn OSAL_InitTasks
 * @brief 初始化OSAL， 添加任务
 * @param none
 * @return none
 */
void OSAL_InitTasks(void)
{
    HAL_Init(taskID++);
    OSAL_TimerInit(taskID++);

}

/**
 * @fn OSAL_SetEvent
 * @brief 给特定taskID任务设置事件
 * @param taskID - 任务对应的ID event - 设置的事件
 * @return none
 */
void OSAL_SetEvent(uint8_t taskID, uint16_t event)
{
    events[taskID] |= event;
}

